<?php

class tag{
	function getList($num){
		global $dbs,$page,$encrypt ;		
		$maxSize = 60 ;
		$minSize = 12 ;
		$step = ( $maxSize - $minSize ) / $num ; 
		$sql = 'SELECT * FROM `'. DB_PRE .'tag_top` ';	
		$sql = 'SELECT * FROM `'. DB_PRE .'tag_top` ORDER BY `count` DESC  limit 0 , '. $num;	
		$lists = $dbs -> select($sql);
		$html = '';
		$fontSize = $maxSize ;
		foreach($lists as $list){			
			//$fontSize = ( $list['count'] / $counts ) * (  $maxSize  -  $minSize    ) + $minSize ;
			//$fontSize = $fontSize - $step  ;
			$fontSize = rand( 12 , 50)  ;
			$margin =  'margin:'.self::getRandMagin().'px '.self::getRandMagin().'px '.self::getRandMagin().'px '.self::getRandMagin().'px';
			$html .='<a style="font-size:'. $fontSize .'px;'.$margin.'" href="'.urlRewrite('option=list&id='.$list['tag_parent_id'].'!'. $list['tag_id']) .'" target="_blank">'. htmlShow($list['tag_name']) .'</a>';			
		}
		$html .='<div class="clear"></div>';
		return $html;
	
	}
	function getRandMagin(){
		return rand( 3 , 10);
	}
	
	
	function buildRank($maxNum = 25 ){
		global $dbs;
		
		$dbs -> delete( DB_PRE .'tag_top');
		$sql = 'select tag,tagMenu,count(`tagMenu`) as `tagMenusCount` FROM `'.DB_PRE.'filetag` group by `tagMenu` ORDER BY `tagMenusCount` DESC limit 0,'.$maxNum;
		$lists = $dbs -> select($sql);
		
		foreach($lists as $list){
			$sql = 'SELECT * FROM `'. DB_PRE .'tag_menu` WHERE `id` = '.$list['tagMenu'];	
			$name = $dbs -> select($sql);
			$name = $name[0];
			$array = '';
			$array['tag_parent_id'] = $list['tag'];
			$array['tag_name'] 		= $name['name'];
			$array['tag_id'] 		= $list['tagMenu'];
			$array['count'] 		= $list['tagMenusCount'];			
			$dbs -> insert(DB_PRE .'tag_top',$array );
		}
	}
	
	function getCustomMenu($tags,$type='file'){
		global $dbs;
		$tags = explode('|',$tags);
		$html = '';
		foreach($tags as $tag){
			$tag = explode('!',$tag);					
			if( 3 != $tag[0] ) continue ;
			$tag = explode(',',$tag[1]);
			foreach($tag as $t ){
				if( 0 == (int)$t ) continue ;
				$sql = 'SELECT * FROM `'.DB_PRE.'tag_menu` WHERE `id` = '. (int)$t ;// .' and `type` = "' . $type . '"'  ;
			 
				$lists = $dbs -> select($sql);
				$list = $lists[0];
				$html .= '<a tag="'. $list['name'] .'" onclick="remove(this);return false;">'. $list['name'] .'</a>';	
			}
		}
		return $html;		
	}

	function saveCustomMenu($tagCustomMenu   ){
		//tagCustomMenus parentId = 3;
		global $dbs;
		$tagCustomMenuFormat = ',';
		$tagCustomMenus = explode(',',$tagCustomMenu);
		
		
		
		foreach($tagCustomMenus as $tagCustom){
			$array = '';
			
			$tagCustom = chop($tagCustom);
			$lists = $dbs -> select('SELECT * FROM `'. DB_PRE.'tag_menu` WHERE `name` = "'. $tagCustom .'" ');
			if(count($lists) == 0){
				//echo $tagCustom .'|';
				if( !checkTagName($tagCustom) || $tagCustom == '' )
					continue; //  不信任客户端，检测字符串是否有 特殊符号.
				//echo $tagCustom .'=';
				$array['name'] = $tagCustom;
				$array['parentId'] = 3;				
				$array['created'] = date('Y-m-d H:i:s');
				
				$tagId = $dbs -> insert( DB_PRE.'tag_menu', $array , true );
				$tagCustomMenuFormat .= ','. $tagId ;
			}else{
				$tagCustomMenuFormat .= ','. $lists[0]['id'] ;
			}
		}
		$tagCustomMenuFormat = str_replace(',,','' , $tagCustomMenuFormat);
		if( $tagCustomMenuFormat == ',') $tagCustomMenuFormat = '';
		return $tagCustomMenuFormat ;
	}
	
	function saveTagMenu($tagMenus,$parentId , $type = 'file'){
		global $dbs;
		$tagMenus = explode('|',$tagMenus);	
		$tag = '';
		$where = ' `parentId` = '. $parentId .' and `type` = "'.$type.'"';
		$dbs -> delete(DB_PRE .'filetag',$where);
	//print_r($tagMenus);
	//exit();
		
		foreach($tagMenus as $tMs){
			$tM = explode('!',$tMs);
			$ids = explode(',',$tM[1]);
			if(''==$tM[1]) continue;
			foreach( $ids as $id){
				if( $id <= 0) continue;
				$tag['parentId'] = $parentId ;
				$tag['tag'] = $tM[0];
				$tag['tagMenu'] = $id;	
				$tag['type'] = $type ;
				$dbs -> insert(DB_PRE.'filetag' ,$tag);			
			}
		}
	}
	
	
}
?>